﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace GUI.mngsys
{
    using EXT;
    using DAL.Entities;
    using BLL;
    using DevExpress.XtraEditors;
    public partial class frmPhanquyen : Form
    {
        public frmPhanquyen()
        {
            InitializeComponent();
        }

        DanhmucBLL Danhmuc_BLL = new DanhmucBLL();
        NhanvienBLL Nhanvien_BLL = new NhanvienBLL();
        ChucnangnhomBLL Chucnangnhom_BLL = new ChucnangnhomBLL();

        DataTable dt = new DataTable();

        public void Load_Danhsach()
        {
            dt = Danhmuc_BLL.Danhmuc_Select_By_Kind_All_Cocha("Chucnang");
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    treeNhanvien.DataSource = dt;
                }
                else
                {
                }
            }
        }

        public void Load_Tree()
        {
            var Nhom = Danhmuc_BLL.Danhmuc_Select_By_Kind_All_Vali("Nhomnguoidung");
            int x = Nhom.Rows.Count;
            int Sonhanvien = 0;
            if (x > 0)
            {
                treeNhanvien.Nodes.Clear();
                for (int i = 0; i < x; i++)
                {
                    TreeListNode node = treeNhanvien.AppendNode(null, null);
                    node.SetValue("Id", Nhom.Rows[i]["Id"].GetGuid());
                    node.SetValue("Name", Nhom.Rows[i]["Name"].ToString());
                    var DsNhanvien = Nhanvien_BLL.Nhanvien_Select_By_Id_Nhomnguoidung(Nhom.Rows[i]["Id"].GetGuid());
                    var y = DsNhanvien.Rows.Count;
                    Sonhanvien += y;
                    if (y > 0)
                    {
                        for (int j = 0; j < y; j++)
                        {
                            TreeListNode chileNode = treeNhanvien.AppendNode(null, node);
                            chileNode.SetValue("Id", DsNhanvien.Rows[j]["Id"].GetGuid());
                            chileNode.SetValue("Name", DsNhanvien.Rows[j]["Name"].ToString());
                        }
                    }
                }
            }
            txtSonhanvien.Text = String.Format("Có {0} nhân viên", Sonhanvien);
        }

        public void Load_Chucnangnhom(Guid Id_Nhomnguoidung)
        {
            var dt = Chucnangnhom_BLL.Chucnangnhom_Select_By_Id_Nhomnguoidung(Id_Nhomnguoidung);
            gcChucnangnhom.DataSource = dt;
            gvChucnangnhom.BestFitColumns();
            if (dt != null)
            {
                int x = dt.Rows.Count;
                if (x > 0)
                {        
                    for (int i = 0; i < x; i++)
                    {
                        DataRow dr = gvChucnangnhom.GetDataRow(i);
                        try
                        {
                            if (dr["Chon"].ToBoolean())
                            {
                                gvChucnangnhom.SetRowCellValue(i, "Chon", "True");
                            }
                            else
                                gvChucnangnhom.SetRowCellValue(i, "Chon", "False");
                        }
                        catch (Exception)
                        {
                            gvChucnangnhom.SetRowCellValue(i, "Chon", "False");
                        }
                    }
                    gvChucnangnhom.BestFitColumns();
                }
            }
        }

        private void frmPhanquyen_Load(object sender, EventArgs e)
        {
            Load_Tree();
        }

        private void treeNhanvien_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            TreeList tree = sender as TreeList;
            if (e.Node == tree.FocusedNode)
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                Rectangle rect = new Rectangle(e.EditViewInfo.ContentRect.Left,
                                                e.EditViewInfo.ContentRect.Top - 2,
                                                Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeNhanvien.Font).Width + 1),
                                                Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeNhanvien.Font).Height));
                e.Graphics.FillRectangle(SystemBrushes.Highlight, rect);
                e.Graphics.DrawString(e.CellText, treeNhanvien.Font, SystemBrushes.HighlightText, rect);
                e.Handled = true;
            }
        }

        private void btnThemNhomnguoidung_Click(object sender, EventArgs e)
        {
            GUI.Danhmuc.ChitietDanhmuc.frmChitietNhomnguoidung frm = new GUI.Danhmuc.ChitietDanhmuc.frmChitietNhomnguoidung();
            frm.MyEvent_LoadData_LoadTree += new MyEvent_LoadData(frm_MyEvent_LoadData_LoadTree);
            frm.ShowDialog();
        }

        void frm_MyEvent_LoadData_LoadTree(object sender, ELoadData e)
        {
            if (e.IsLoadData)
            {
                Load_Tree();
            }
        }

        private void btnThemnhanvien_Click(object sender, EventArgs e)
        {
            GUI.mngsys.Chitietmnsys.frmChitietNhanvien frm = new GUI.mngsys.Chitietmnsys.frmChitietNhanvien();
            frm.MyEvent_LoadData_LoadTreeNV += new MyEvent_LoadData(frm_MyEvent_LoadData_LoadTreeNV);
            frm.ShowDialog();
        }

        void frm_MyEvent_LoadData_LoadTreeNV(object sender, ELoadData e)
        {
            if (e.IsLoadData)
            {
                Load_Tree();
            }
        }

        public Guid Id_Nhomnguoidung = Guid.Empty;

        private void treeNhanvien_AfterFocusNode(object sender, NodeEventArgs e)
        {
            Id_Nhomnguoidung = e.Node.GetValue("Id").GetGuid();
            Load_Chucnangnhom(Id_Nhomnguoidung);
        }

        private void ckbChonAll_CheckedChanged(object sender, EventArgs e)
        {
            int x = gvChucnangnhom.DataRowCount;
            for (int i = 0; i < x; i++)
            {
                gvChucnangnhom.SetRowCellValue(i, "Chon", ckbChonAll.EditValue.ToBoolean().ToString());
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có chắc muốn lưu những phân quền này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Chucnangnhom_BLL._sqlMapper.BeginTransaction();
                try
                {
                    Chucnangnhom_BLL.Chucnangnhom_Delete_By_Id_Nhomnguoidung(Id_Nhomnguoidung);
                    int x = gvChucnangnhom.DataRowCount;
                    for (int i = 0; i < x; i++)
                    {
                        if (gvChucnangnhom.GetRowCellValue(i, "Chon").ToBoolean())
                        {
                            var cnn = new Chucnangnhom();
                            cnn.Id = Guid.NewGuid();
                            cnn.Id_Nhomnguoidung = Id_Nhomnguoidung;
                            cnn.Id_Chucnang = gvChucnangnhom.GetRowCellValue(i, "Id").GetGuid();
                            cnn.Chon = true;
                            Chucnangnhom_BLL.Chucnangnhom_Insert(cnn);
                        }
                    }
                    Chucnangnhom_BLL._sqlMapper.CommitTransaction();
                    Load_Chucnangnhom(Id_Nhomnguoidung);
                    XtraMessageBox.Show("Lưu phân quyền thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    Chucnangnhom_BLL._sqlMapper.RollBackTransaction();
                }
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThemchucnang_Click(object sender, EventArgs e)
        {
            var frm = new GUI.Danhmuc.frmChucnang();
            frm.MyEvent_LoadData_Chitiet += new MyEvent_LoadData(frm_MyEvent_LoadData_Chitiet);
            frm.ShowDialog();
        }

        void frm_MyEvent_LoadData_Chitiet(object sender, ELoadData e)
        {
            if (e.IsLoadData)
            {
                Load_Chucnangnhom(Id_Nhomnguoidung);
            }
        }
    }
}
